#coding:utf-8
"""
*   Coder    : Dzlua
*   Email    : 505544956@qq.com
*   Time     : 2017/09/06
"""
#--------------------#
from utilities import helper
import prots
#--------------------#
from models.users import User
#--------------------#
class So(helper.Resource):
    decorators = [helper.auth.login_required]
    def __init__(self):
        self.reqparse = helper.reqparse.RequestParser()
        self.reqparse.add_argument('libname', type = str, required = True,
            help = 'No libname provided')
        self.reqparse.add_argument('content', type = str, required = True,
            help = 'No content provided')
        super(So, self).__init__()
    def post(self):
        args = self.reqparse.parse_args()
        code, msg = prots.so(**args)
        if code==0:
            js_str = msg['res']
            msg['res'] = helper.js_str_to_dict(js_str, js_str)
        return helper.make_res(code, args=msg)
#----------#
class Dir(helper.Resource):
    def get(self):
        data = {
            'protocol': prots.dir(),
            'args': helper.request.args,
            'url': helper.url_for('dir') }
        return helper.jsonify(data)
#----------#
class File(helper.Resource):
    def get(self):
        data = {
            'protocol': prots.file(),
            'args': helper.request.args,
            'url': helper.url_for('file') }
        return helper.jsonify(data)
#----------#
class Select(helper.Resource):
    def get(self):
        data = {
            'protocol': prots.select(),
            'args': helper.request.args,
            'url': helper.url_for('select') }
        return helper.jsonify(data)
#--------------------#
class Register(helper.Resource):
    def __init__(self):
        self.reqparse = helper.reqparse.RequestParser()
        self.reqparse.add_argument('username', type = str, required = True,
            help = 'No username provided')
        self.reqparse.add_argument('password', type = str, required = True,
            help = 'No password provided')
    def post(self):
        args = self.reqparse.parse_args()
        username = args.get('username')
        password = args.get('password')
        #
        if User.query.filter_by(username=username).first() is not None:
            return helper.make_res(1, {'username': username})
        #
        user = User(username=username)
        user.hash_password(password)
        helper.db.session.add(user)
        helper.db.session.commit()
        #
        return (helper.make_res(0, {'username': user.username}),
                201, 
                {'Location': helper.url_for('get_user', id=user.id, _external=True)} )
#--------------------#
class GetUser(helper.Resource):
    decorators = [helper.auth.login_required]
    def __init__(self):
        self.reqparse = helper.reqparse.RequestParser()
        self.reqparse.add_argument('userid', type = str, required = True,
            help = 'No userid provided')
    def get(self):
        args = self.reqparse.parse_args()
        id = args.get('userid')
        user = User.query.get(id)
        if not user:
            return helper.make_res(2, {'id': id})
        return helper.make_res(0, {'id': id, 'username': user.username})
    def post(self):
        return self.get()
#--------------------#
class Users(helper.Resource):
    decorators = [helper.auth.login_required]
    def __init__(self):
        self.reqparse = helper.reqparse.RequestParser()
    def get(self):
        args = self.reqparse.parse_args()
        begin = args.get('begin') or 0
        end = args.get('end') or 10
        users = User.query.filter('id>=:begin and id<=:end').params({'begin': begin, 'end': end}).all()
        data = { 'count': len(users), 'users': []}
        for user in users:
            info = {'id': user.id, 'username': user.username}
            data['users'].append(info)
        return helper.make_res(0, data)
    def post(self):
        return self.get()
#--------------------#
class Token(helper.Resource):
    decorators = [helper.auth.login_required]
    def post(self):
        token = helper.g.user.generate_auth_token(600)
        return helper.make_res(0, {'token': token.decode('ascii'), 'duration': 600})
    def get(self):
        return self.post()
#--------------------#
        